#!/bin/bash
#
# This script installs prerequisites for Latisi. It is written to run on
# Mac OS X Leopard (10.5) and Snow Leopard (10.6).
#
# In case of Latisi compilation problems, try uninstalling some gems:
# gem uninstall -v 2.3.5 actionmailer actionpack activerecord activeresource activesupport
#
# Before running this script, make sure that the following software is
# installed: - MacPorts
#            - Subversion
#            - Flash Player
#
MYSQL=1
FLEX=0
INSTALL_FAIL="*** Latisi installation failed"
PW=`pwd`
LOGFILE="$PW/latisi_install_log"

# Process arguments, die with use message if wrong
if [ $# -gt 0 ]
then
    ARGC=$#
    ARGS=("$@")
    for (( i=0; i<$ARGC; i++));
    do
	echo ${ARGS[$i]}
	if [ ${ARGS[$i]} = "--nomysql" ]
	then
	    MYSQL=0
	elif [ ${ARGS[$i]} = "--withflex" ]
	then
	    FLEX=1
	else
	    echo "Usage: sudo ./installscript [--nomysql] [--withflex]"
	    echo "(the two arguments are optional)"
	    exit 3
	fi
    done
fi



# Check that MacPorts is installed
PORT_CHECK_CMD="port help"
$PORT_CHECK_CMD > /dev/null 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Cannot find the MacPorts \"port\" command."
    echo "It should be found via your PATH, and is usually installed in /opt/local/bin"
    echo "If MacPorts is installed, please add to your PATH with the command"
    echo "      export PATH=\$PATH:/opt/local/bin"
    echo "Otherwise, install MacPorts: http://www.macports.org/install.php"
    exit 1
fi



# Install Ruby
echo "Installing Latisi at `date`" > $LOGFILE
echo "Installing ruby - this may take a while..."
echo "Installing ruby" >> $LOGFILE
INSTALL_RUBY_CMD="port install ruby"
$INSTALL_RUBY_CMD >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install Ruby check the file $LOGFILE for information"
    exit 1
fi

# Expand the path to include ruby
echo "Expanding the PATH to include the newly installed ruby executable..."
echo "Expanding the PATH to include the newly installed ruby executable..." >> $LOGFILE
export PATH=$PATH:/opt/local/var/macports/software/ruby/1.8.7-p249_1+darwin+thread_hooks/opt/local/bin

# Install a symbolic link so that brew can work with ruby
echo "Installing a symbolic link to the new ruby in /usr/bin..."
echo "Installing a symbolic link to the new ruby in /usr/bin..." >> $LOGFILE
ln -s /opt/local/var/macports/software/ruby/1.8.7-p249_1+darwin+thread_hooks/opt/local/bin/ruby /usr/bin/ruby




# Install MySQL
if [ $MYSQL -eq 1 ]
then
    echo "Installing MySQL, this could take a while..."
    echo "Installing MySQL with MacPorts into directory /opt/local" >> $LOGFILE
    port install mysql5 >> $LOGFILE 2>&1
    if [ $? -ne 0 ]
    then
	echo $INSTALL_FAIL
	echo "Unable to install MySQL check the file $LOGFILE for information"
	exit 1
    fi
    
    # Assuming that the installation of MySQL completed successfully, we have to
    # set up permissions and make a new directory for it to run.
    echo "Setting up MySQL tables..."
    echo "Setting up MySQL tables with /opt/local/lib/mysql5/bin/mysql_install_db..." >> $LOGFILE
    /opt/local/lib/mysql5/bin/mysql_install_db >> $LOGFILE 2>&1
    if [ $? -ne 0 ]
    then
	echo $INSTALL_FAIL
	echo "Unable to install MySQL initial tables - check the file $LOGFILE for information"
	exit 1
    fi
    
    echo "Changing permissions for MySQL directories..."
    echo "Changing permissions for MySQL directories..." >> $LOGFILE
    mkdir -p /opt/local/var/run/mysql5 >> $LOGFILE 2>&1
    if [ $? -ne 0 ]
    then
	echo $INSTALL_FAIL
	echo "Unable to make additional directories for MySQL - check the file $LOGFILE for information"
	exit 1
    fi

    chmod -R og+wrx /opt/local >> $LOGFILE 2>&1
    if [ $? -ne 0 ]
    then
	echo $INSTALL_FAIL
	echo "Unable to change directory permissions for MySQL directories - check the file $LOGFILE for information"
	exit 1
    fi
    
    echo "Starting MySQL server with /opt/local/lib/mysql5/bin/mysqld_safe &"
    echo "Starting MySQL server with /opt/local/lib/mysql5/bin/mysqld_safe &" >> $LOGFILE 2>&1
    /opt/local/lib/mysql5/bin/mysqld_safe & >> $LOGFILE 2>&1
    
    # Wait a few seconds for it to properly start up
    sleep 9
fi



# Check for pkg-config, install if not found
echo "Checking for pkg-config..."
echo "Checking for pkg-config..." >> $LOGFILE
pkg-config 2>&1 1> /dev/null | grep -i 'command not found' | wc > pkg-config-check
grep "       0       0       0" pkg-config-check >> $LOGFILE
if [ $? -ne 0 ]
then
    echo "pkg-config not found - installing with macports..."
    echo "pkg-config not found - installing with macports..." >> $LOGFILE
    port install pkgconfig
    if [ $? -ne 0 ]
    then
	echo $INSTALL_FAIL
	echo "Unable to install pkg-config, which is required for TiMBL - check the file $LOGFILE for information"
	exit 1
    fi
fi

# Remove temporary file
rm pkg-config-check



# Should now be able to use pgk-config to check for libxml2
echo "Checking for libxml2..." >> $LOGFILE
pkg-config --exists --print-errors libxml-2.0 >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo "libxml2 not found - installing with macports..."
    echo "libxml2 not found - installing with macports..." >> $LOGFILE
    port install libxml2
    if [ $? -ne 0 ]
    then
	echo $INSTALL_FAIL
	echo "Unable to install libxml2, which is required for TiMBL - check the file $LOGFILE for information"
	exit 1
    fi
fi



# Install timbl
echo "Installing TiMBL..."
echo "Installing TiMBL..." >> $LOGFILE
mkdir timbl
cd timbl
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to create directory for TiMBL download - check the file $LOGFILE for information"
    exit 1
fi

# curl should be installed by default on Mac OS X according to their homepage.
curl -f http://ilk.uvt.nl/downloads/pub/software/timbl-6.2.1.tar.gz > timbl-6.2.1.tar.gz 2>> $LOGFILE
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to download TiMBL from http://ilk.uvt.nl/downloads/pub/software/timbl-6.2.1.tar.gz - check the file $LOGFILE for information"
    exit 1
fi


tar -xzf timbl-6.2.1.tar.gz
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to extract TiMBL archive - check the file $LOGFILE for information"
    exit 1
fi

cd timbl-6.2.1
./configure >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to configure TiMBL installation - check the file $LOGFILE for information"
    exit 1
fi

make >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to build TiMBL - check the file $LOGFILE for information"
    exit 1
fi

make install >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install TiMBL after building it - check the file $LOGFILE for information"
    exit 1
fi
cd $PW

# Remove the temporary TiMBL directory
rm -rf timbl

# Install the gem tool for Ruby
echo "Installing gem..."
echo "Installing gem..." >> $LOGFILE
sudo port install rb-rubygems >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install gems - check the file $LOGFILE for information"
    exit 1
fi




# Install Flex SDK
if [ $FLEX -eq 1 ]
then
    mkdir flexdownload
    chmod -R og+wrx *
    echo "Installing Flex SDK in flexdownload/..."
    echo "Installing Flex SDK in flexdownload/..." >> $LOGFILE
    cd flexdownload
    sudo curl -f http://download.macromedia.com/pub/flex/sdk/flex_sdk_3.5.zip > flex_sdk_3.5.zip 2>>$LOGFILE
    if [ $? -ne 0 ]
    then
	echo $INSTALL_FAIL
	echo "Unable to download Flex SDK from http://download.macromedia.com/pub/flex/sdk/flex_sdk_3.5.zip - check the file $LOGFILE for information"
	exit 1
    fi
    
    unzip flex_sdk_3.5.zip >> $LOGFILE 2>&1
    if [ $? -ne 0 ]
    then
	echo $INSTALL_FAIL
	echo "Unable to unzip Flex SDK zip file - check the file $LOGFILE for information"
	exit 1
    fi

    # Add Flex SDK to PATH
    FLEXBIN=`pwd`
    FLEXBIN="$FLEXBIN/bin"
    echo "Adding Flex executables to PATH. To make this permanent, add the command"
    echo "     export PATH=\$PATH:$FLEXBIN"
    echo "to your .profile"
    echo "Adding Flex executables to PATH. To make this permanent, add the command" >> $LOGFILE
    echo "     export PATH=\$PATH:$FLEXBIN" >> $LOGFILE
    echo "to your .profile" >> $LOGFILE
    export PATH=$PATH:/Users/ipcuser/Desktop/flexdownload/bin
    cd $PW
fi



# Install ImageMagick
echo "Installing ImageMagick..."
echo "Installing ImageMagick..." >> $LOGFILE
port install ImageMagick  >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install ImageMagick check the file $LOGFILE for information"
    exit 1
fi




# Install gems
echo "Installing required gems - this may take a while..."
echo "Installing required gems - this may take a while..." >> $LOGFILE
gem install --no-rdoc --no-ri mysql >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri aasm >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri hoe >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri paperclip >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri rmagick >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri sqlite3-ruby >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri restfulx >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri nokogiri >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri rtimbl >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri rake >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi
gem install --no-rdoc --no-ri daemon-spawn --source http://gemcutter.org >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
    echo $INSTALL_FAIL
    echo "Unable to install all of the required gems - check the file $LOGFILE for information"
    exit 1
fi



# Everything seems to work, return 0
echo "Latisi installation completed"
echo "Latisi installation completed at `date`" >> $LOGFILE
exit 0
